<?php

class App_Methods_Request_SaveUserPulpit {

  /**
   *
   * @var App_Methods_Data_App
   */
  public $app;
  public $applicationId;
  public $data;

  public function __construct($params) {
    $this->app = new App_Methods_Data_App($params->app);
    $this->applicationId = $params->applicationId;
    $this->data = $params->getDefault('data');
  }

  public function process() {
    $userPulpit = UserPulpitQuery::create()->findPk(array(Core_Auth::getInstance()->getUser()->getId(), $this->applicationId));
    if (!$userPulpit instanceof UserPulpit) {
    	$userPulpit = new UserPulpit();
    	$userPulpit->setDefaultData();
    }
  	foreach ($this->data as $data) {
  		$userPulpit->setByName($data['name'], $data['value'], BasePeer::TYPE_FIELDNAME);
  	}
  	$userPulpit->save();
    return new App_Methods_Response_SaveUserPulpit(true, new App_Methods_Data_UserPulpit($userPulpit));
  }

}